package com.smmisia.article.controller;

import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.smmisia.article.dto.article.ArticleAddRequest;
import com.smmisia.article.dto.article.ArticleHotResponse;
import com.smmisia.article.dto.article.ArticleUpdateDTO;
import com.smmisia.article.dto.likeFavorite.LikeFavoriteRequest;
import com.smmisia.article.response.RankResponse;
import com.smmisia.article.response.VisitorResponse;
import com.smmisia.article.response.article.ArticleResponse;
import com.smmisia.article.service.ArticleService;
import com.smmisia.common.Result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@Tag(name = "文章管理")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页查询 获取我的游记或攻略
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询 获取我的游记或攻略")
    public Result<List<ArticleResponse>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                   @RequestParam(required = false) Integer id,
                                                                                   @RequestParam(value = "type") @NotNull(message = "类型不能为空") Integer type) {
        return articleService.listArticle(pageNum, pageSize, id, type);
    }

    /**
     * 分页查询 获取我的文章
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/articleAll")
    @Operation(summary = "分页查询 获取用户文章")
    public Result<List<ArticleResponse>> listArticle(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                              @RequestParam(required = false) Integer id
                                              ) {
        return articleService.article(pageNum, pageSize, id);
    }

    /**
     * 发布游记或攻略(定时发布)
     *
     * @param articleAddRequest
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "发布游记或攻略(定时发布)")
    public Result<String> add(@Valid @RequestBody ArticleAddRequest articleAddRequest) {
        return articleService.add(articleAddRequest);
    }


    /**
     * 修改游记或攻略
     *
     * @param articleUpdateDTO
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "修改游记或攻略")
    public Result<String> update(@Valid @RequestBody ArticleUpdateDTO articleUpdateDTO) {
        return articleService.updateArticle(articleUpdateDTO);
    }

    /**
     * 修改游记或攻略
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @Operation(summary = "删除游记或攻略")
    public Result<String> deletes(@RequestParam("id") Integer id) {
        return articleService.deletes(id);
    }

    /**
     * 获取热门攻略或者游记
     */
    @PostMapping("/hotspot")
    @Operation(summary = "获取热门攻略或者游记")
    public Result<ArticleHotResponse> hotspot() {
        //TODO 返回结果有误
        return articleService.hotspot();
    }

    /**
     * 游记或攻略点赞
     *
     * @param likeFavoriteRequest
     * @return
     */
    @PostMapping("/like")
    @Operation(summary = "游记或攻略点赞")
    public Result<String> like(@Valid @RequestBody LikeFavoriteRequest likeFavoriteRequest) {
        return articleService.like(likeFavoriteRequest);
    }

    /**
     * 游记或攻略收藏
     *
     * @param likeFavoriteRequest
     * @return
     */
    @PostMapping("/favorite")
    @Operation(summary = "游记或攻略收藏")
    public Result<String> favorite(@Valid @RequestBody LikeFavoriteRequest likeFavoriteRequest) {
        return articleService.favorite(likeFavoriteRequest);

    }

    /**
     * 游记或攻略收藏
     *
     * @return
     */
    @GetMapping("/likeFavoriteInfo")
    @Operation(summary = "获取我的点赞收藏列表详情")
    public Result<List<ArticleResponse>> likeInfo(@RequestParam(required = false) Integer id,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "type") Integer type) {
        return articleService.likeInfo(id, pageNum, pageSize, type);
    }

    /**
     * 获取朋友圈文章
     */
    @GetMapping("/friend")
    @Operation(summary = "获取朋友圈文章")
    public Result<List<ArticleResponse>> articleFriendList() {
        return articleService.articleFriendList();
    }

    /**
     * 获取我的关注文章
     */
    @GetMapping("/follow")
    @Operation(summary = "获取我的关注文章")
    public Result<List<ArticleResponse>> articleFollowList() {
        return articleService.articleFollowList();
    }

    /**
     * 文章模糊查询
     */
    @GetMapping("/likeSearch")
    @Operation(summary = "文章模糊查询")
    public Result<List<ArticleResponse>> likeSearch(@Valid @RequestParam String keyword) {
        return articleService.likeSearch(keyword);
    }

    /**
     * 多线程实现百万导出到excel 50s优化到11s
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    @Operation(summary = "多线程文章导出")
    public void export(HttpServletResponse response) throws IOException {
        articleService.export(response);
    }

    /**
     * 单线程实现百万导出到excel 50s优化到11s
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/newExport")
    @Operation(summary = "单线程文章导出")
    public void newExport(HttpServletResponse response) throws IOException {
        articleService.newExport(response);
    }
    /**
     * 根据id获取文章
     *
     * @return
     */
    @GetMapping("/articleById")
    @Operation(summary = "根据id获取文章")
    public Result<List<ArticleResponse>> articleById(@RequestParam Integer id) {
        return articleService.articleById(id);
    }

    /**
     * 分页查询 获取所有文章
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/articleList")
    @Operation(summary = "分页查询 获取所有文章")
    public Result<List<ArticleResponse>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize) {
        return articleService.articleList(pageNum, pageSize);
    }


    /**
     * 分页查询 获取所有攻略
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/articleNotes")
    @Operation(summary = "分页查询 获取所有攻略")
    public Result<List<ArticleResponse>> listArticleNotes(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                                               @RequestParam(defaultValue = "10") Integer pageSize) {
        return articleService.listArticleNotes(pageNum, pageSize);
    }

    /**
     * 分页查询 获取所有游记
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/articleStrategy")
    @Operation(summary = "分页查询 获取所有游记")
    public Result<List<ArticleResponse>> listArticleStrategy(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return articleService.listArticleStrategy(pageNum, pageSize);
    }

    /**
     * 隐藏文章
     *
     * @param id
     * @return
     */
    @GetMapping("/hidden")
    @Operation(summary = "隐藏文章")
    public Result<String> hidden(@RequestParam("id") Integer id) {
        return articleService.hidden(id);
    }

    @GetMapping("/visitor")
    @Operation(summary = "获取游客访问量")
    public Result<VisitorResponse> visitor() {
        return articleService.visitor();
    }

    @GetMapping("/rank")
    @Operation(summary = "获取排行榜")
    public Result<List<RankResponse>> rank() {
        return articleService.rank();
    }

    @GetMapping("/articleEs")
    @Operation(summary = "模糊查询")
    public Result<List<ArticleResponse>> articleEs(@RequestParam String keyword,Integer pageNum,Integer pageSize,Integer lastId) {
        return articleService.articleEs(keyword,pageNum,pageSize,lastId);
    }

}
